"""
使用Python进行邮件发送
smtplib模块：主要进行发送邮件
# 导入Python内置的smtplib模块，用于发送电子邮件
import smtplib

# 使用smtplib.SMTP类创建一个SMTP对象，指定邮件服务器的主机名（host）和端口号（port）

host:连接邮箱的服务器，可以指定服务器的域名（QQ邮箱服务器：smtp.qq.com;网易邮箱服务器：smtp.163.com等等）
port:是端口，一般情况下smtp默认端口号为25。（QQ邮箱端口号：465或587，网易邮箱端口号：465或994）

smtp = smtplib.SMTP_SSL(host=host, port=port)

# 登录SMTP服务器，提供用户名（username）和密码（password）进行身份验证
# 进行登录，需要：发送方邮箱地址和密码（邮箱地址的授权码）
smtp.login(username=username, password=password)

# 准备发送邮件，参数说明如下：
# from_addr：发件人电子邮件地址
# to_addrs：收件人电子邮件地址（可以是单个地址字符串，也可以是包含多个地址的列表）
# msg.as_string()：邮件消息体，通常由email库构造并转换为字符串形式
smtp.sendmail(from_addr=from_addr, to_addrs=to_addrs, msg=msg.as_string())

# 发送完毕后，关闭与SMTP服务器的连接，释放资源
smtp.quit()

email模块：主要进行构建邮件
from email.mime.text import MIMEText #纯文本，HTML
from email.mime.image import MIMEImage #图片
from email.mime.multipart import MIMEMultipart #多种组合内容




"""
# import smtplib
# from email.mime.text import MIMEText
# from email.header import Header
#
# from_addr = "1976681425@qq.com"
# password = "deudxfpboxrhegdj"
# to_addr = "1976681425@qq.com"
# host = "smtp.qq.com"
# port = 465
#
# msg = MIMEText("使用Python进行发送纯文本邮件","plain","utf-8")
# msg["From"] = Header(from_addr)
# msg["To"] = Header(to_addr)
# msg["Subject"] = Header("发送纯文本邮件")
#
# try:
#     smtp = smtplib.SMTP_SSL(host, port)
#     smtp.login(from_addr, password)
#     smtp.sendmail(from_addr, to_addr, msg.as_string())
#     smtp.quit()
#     print("邮件发送成功")
# except smtplib.SMTPException as e:
#     print("邮件发送失败", e)

# import smtplib
# from email.mime.text import MIMEText
# from email.header import Header
#
# from_addr = "1976681425@qq.com"
# password = "deudxfpboxrhegdj"
# to_addr = "1976681425@qq.com"
# host = "smtp.qq.com"
# port = 465
#
# html_file = """
# <a href='https://www.jd.com'>京东商城</a>
# """
#
# msg = MIMEText(html_file,"html","utf-8")
# msg["From"] = Header(from_addr)
# msg["To"] = Header(to_addr)
# msg["Subject"] = Header("发送html邮件")
#
# try:
#     smtp = smtplib.SMTP_SSL(host, port)
#     smtp.login(from_addr, password)
#     smtp.sendmail(from_addr, to_addr, msg.as_string())
#     smtp.quit()
#     print("邮件发送成功")
# except smtplib.SMTPException as e:
#     print("邮件发送失败", e)

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

from_addr = "1976681425@qq.com"
password = "deudxfpboxrhegdj"
to_addr = "1976681425@qq.com"
host = "smtp.qq.com"
port = 465

msg = MIMEMultipart()

html_file = """
<h2>附件邮件</h2>
"""
m1 = MIMEText(html_file,'html','utf-8')
msg.attach(m1)

m2 = MIMEText(open('a.txt', 'rb').read(), 'base64', 'utf-8')
m2["Content-Type"] = 'application/octet-stream'
m2["Content-Disposition"] = 'attachment;filename="a.txt"'
msg.attach(m2)

m3 = MIMEText(open('user.xlsx', 'rb').read(), 'base64', 'utf-8')
m3["Content-Type"] = 'application/octet-stream'
m3["Content-Disposition"] = 'attachment;filename="user.xlsx"'
msg.attach(m3)

msg['From'] = Header(from_addr)
msg['To'] = Header(to_addr)
msg['Subject'] = Header('附件邮件')

try:
    smtp = smtplib.SMTP_SSL(host, port)
    smtp.login(from_addr, password)
    smtp.sendmail(from_addr, to_addr, msg.as_string())
    smtp.quit()
    print("邮件发送成功")
except Exception as e:
    print("邮件发送失败", e)













